import sdk from '../core';
import component, { ComType } from "../core/tool/component";
import { wordCell, wordMap } from "../core/rpg/wordMap";
import { analysisIntent, baseScene } from "../core/tool/baseScene";
import { SignService } from "../service/SignService";
import { ModernSignService } from "../service/ModernSignService";
import { TaskService } from "../service/TaskService";
import { Bag } from '../core/bag';

/**
 * 签到组件 - 现代化组件，使用依赖注入
 * 职责：
 * 1. 管理签到数据和状态
 * 2. 存储和维护玩家签到数据
 * 3. 处理签到指令和状态查询
 * 4. 通过注入的ModernSignService处理业务逻辑
 */
export default class sign_component extends component {
    private _modernSignService: ModernSignService;
    signData: any = {};

    constructor() {
        super();
        // 使用依赖注入获取ModernSignService（_开头，不会被持久化）
        this._modernSignService = this.getService<ModernSignService>('ModernSignService');
    }

    init() {
        
    }

    // 签到指令处理
    async client_sign(data: analysisIntent) {
        const result = await this._modernSignService.sign(data.player);
        
        if (result.success) {
            let card = sdk.tool.card.create();
            card.title('签到成功');
            card.l(`连续签到:${result.signData.consecutiveDays}天`);
            card.l(`总签到数:${result.signData.totalSignDays}天`);
            // 显示获得的奖励
            if (result.rewards && result.rewards.length > 0) {
                // 使用Bag来生成奖励文本
                const rewardBag = new Bag();
                result.rewards.forEach(reward => {
                    rewardBag.addItem(reward);
                });
                
                // 合并奖励卡片
                card.merge(rewardBag.getCard('🎁签到奖励'));
            }
            
            this.client_text(card.getText());
            
            // 显示任务完成提示
            if (result.completedTasks && result.completedTasks.length > 0) {
                let taskCard = sdk.tool.card.create();
                taskCard.title('🎉任务完成!');
                
                result.completedTasks.forEach((task: any) => {
                    const taskConfig = TaskService.getTaskConfigs().find((t: any) => t.id === task.taskId);
                    if (taskConfig) {
                        taskCard.l(`✅${taskConfig.name} 已完成!`);
                        
                        // 使用Bag来生成任务奖励文本，参考签到奖励的写法
                        if (taskConfig.rewards && taskConfig.rewards.length > 0) {
                            const taskRewardBag = new Bag();
                            taskConfig.rewards.forEach((reward: any) => {
                                // 创建道具对象
                                const prop = sdk.prop.createProp({
                                    type: reward.type,
                                    id: reward.id,
                                    count: reward.count
                                });
                                taskRewardBag.addItem(prop);
                            });
                            
                            // 合并任务奖励卡片
                            taskCard.merge(taskRewardBag.getCard('🎁任务奖励'));
                        }
                    }
                });
                
                this.client_text(taskCard.getText());
            }
        } else {
            this.client_text(result.message);
        }
    }

    /**
     * 获取签到数据
     */
    getSignData(): any {
        return this.signData;
    }

    /**
     * 设置签到数据
     */
    setSignData(data: any): void {
        this.signData = data;
    }
}

